﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MemCustomField
    {
        public int Add(Chain.Model.MemCustomField model)
        {
            string str = (model.CustomType == 1) ? "Mem" : "Goods";
            string customField = model.CustomField;
            string sQLString = "Alter Table " + str + " Add " + model.CustomField + "  Varchar(255)";
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemCustomField(");
            builder.Append("CustomType,CustomField,CustomFieldName,CustomFieldIsNull,CustomFieldIsShow,CustomFieldType,CustomFieldInfo,CustomFieldShopID,CustomFieldCreateTime,CustomFieldUserID)");
            builder.Append(" values (");
            builder.Append("@CustomType,@CustomField,@CustomFieldName,@CustomFieldIsNull,@CustomFieldIsShow,@CustomFieldType,@CustomFieldInfo,@CustomFieldShopID,@CustomFieldCreateTime,@CustomFieldUserID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomType", SqlDbType.Int), new SqlParameter("@CustomField", SqlDbType.VarChar, 50), new SqlParameter("@CustomFieldName", SqlDbType.NVarChar, 50), new SqlParameter("@CustomFieldIsNull", SqlDbType.Bit, 1), new SqlParameter("@CustomFieldIsShow", SqlDbType.Bit, 1), new SqlParameter("@CustomFieldType", SqlDbType.VarChar, 50), new SqlParameter("@CustomFieldInfo", SqlDbType.VarChar, 500), new SqlParameter("@CustomFieldShopID", SqlDbType.Int, 4), new SqlParameter("@CustomFieldCreateTime", SqlDbType.DateTime), new SqlParameter("@CustomFieldUserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CustomType;
            cmdParms[1].Value = model.CustomField;
            cmdParms[2].Value = model.CustomFieldName;
            cmdParms[3].Value = model.CustomFieldIsNull;
            cmdParms[4].Value = model.CustomFieldIsShow;
            cmdParms[5].Value = model.CustomFieldType;
            cmdParms[6].Value = model.CustomFieldInfo;
            cmdParms[7].Value = model.CustomFieldShopID;
            cmdParms[8].Value = model.CustomFieldCreateTime;
            cmdParms[9].Value = model.CustomFieldUserID;
            DbHelperSQL.ExecuteSql(sQLString);
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CustomFieldID)
        {
            Chain.Model.MemCustomField model = this.GetModel(CustomFieldID);
            string str = (model.CustomType == 1) ? "Mem" : "Goods";
            string customField = model.CustomField;
            string sQLString = "Alter Table " + str + " Drop Column " + model.CustomField;
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemCustomField ");
            builder.Append(" where CustomFieldID=@CustomFieldID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomFieldID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CustomFieldID;
            DbHelperSQL.ExecuteSql(sQLString);
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CustomFieldIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemCustomField ");
            builder.Append(" where CustomFieldID in (" + CustomFieldIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(string CustomField)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemCustomField");
            builder.Append(" where CustomField = @CustomField ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomField", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = CustomField;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int CustomFieldID, string CustomFieldName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemCustomField");
            builder.Append(" where CustomFieldName=@CustomFieldName and  CustomFieldID<>@CustomFieldID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomFieldName", SqlDbType.NVarChar, 50), new SqlParameter("@CustomFieldID", SqlDbType.Int) };
            cmdParms[0].Value = CustomFieldName;
            cmdParms[1].Value = CustomFieldID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetAllCustom()
        {
            string sQLString = "select MemCustomField.* ,(select ShopName from SysShop where MemCustomField.CustomFieldShopID = SysShop.ShopID) as ShopName,(select UserName from SysUser where MemCustomField.CustomFieldUserID = SysUser.UserID) as UserName from MemCustomField";
            return DbHelperSQL.Query(sQLString);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CustomFieldID,CustomType,CustomField,CustomFieldName,CustomFieldIsNull,CustomFieldIsShow,CustomFieldType,CustomFieldInfo,CustomFieldShopID,CustomFieldCreateTime,CustomFieldUserID ");
            builder.Append(" FROM MemCustomField ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CustomFieldID,CustomType,CustomField,CustomFieldName,CustomFieldIsNull,CustomFieldIsShow,CustomFieldType,CustomFieldInfo,CustomFieldShopID,CustomFieldCreateTime,CustomFieldUserID ");
            builder.Append(" FROM MemCustomField ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.CustomFieldID desc");
            }
            builder.Append(")AS Row, T.*  from MemCustomField T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CustomFieldID", "MemCustomField");
        }

        public Chain.Model.MemCustomField GetModel(int CustomFieldID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 CustomFieldID,CustomType,CustomField,CustomFieldName,CustomFieldIsNull,CustomFieldIsShow,CustomFieldType,CustomFieldInfo,CustomFieldShopID,CustomFieldCreateTime,CustomFieldUserID from MemCustomField ");
            builder.Append(" where CustomFieldID=@CustomFieldID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomFieldID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CustomFieldID;
            Chain.Model.MemCustomField field = new Chain.Model.MemCustomField();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["CustomFieldID"] != null) && (set.Tables[0].Rows[0]["CustomFieldID"].ToString() != ""))
            {
                field.CustomFieldID = int.Parse(set.Tables[0].Rows[0]["CustomFieldID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomType"] != null) && (set.Tables[0].Rows[0]["CustomType"].ToString() != ""))
            {
                field.CustomType = int.Parse(set.Tables[0].Rows[0]["CustomType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomField"] != null) && (set.Tables[0].Rows[0]["CustomField"].ToString() != ""))
            {
                field.CustomField = set.Tables[0].Rows[0]["CustomField"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomFieldName"] != null) && (set.Tables[0].Rows[0]["CustomFieldName"].ToString() != ""))
            {
                field.CustomFieldName = set.Tables[0].Rows[0]["CustomFieldName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomFieldIsNull"] != null) && (set.Tables[0].Rows[0]["CustomFieldIsNull"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["CustomFieldIsNull"].ToString() == "1") || (set.Tables[0].Rows[0]["CustomFieldIsNull"].ToString().ToLower() == "true"))
                {
                    field.CustomFieldIsNull = true;
                }
                else
                {
                    field.CustomFieldIsNull = false;
                }
            }
            if ((set.Tables[0].Rows[0]["CustomFieldIsShow"] != null) && (set.Tables[0].Rows[0]["CustomFieldIsShow"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["CustomFieldIsShow"].ToString() == "1") || (set.Tables[0].Rows[0]["CustomFieldIsShow"].ToString().ToLower() == "true"))
                {
                    field.CustomFieldIsShow = true;
                }
                else
                {
                    field.CustomFieldIsShow = false;
                }
            }
            if ((set.Tables[0].Rows[0]["CustomFieldType"] != null) && (set.Tables[0].Rows[0]["CustomFieldType"].ToString() != ""))
            {
                field.CustomFieldType = set.Tables[0].Rows[0]["CustomFieldType"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomFieldInfo"] != null) && (set.Tables[0].Rows[0]["CustomFieldInfo"].ToString() != ""))
            {
                field.CustomFieldInfo = set.Tables[0].Rows[0]["CustomFieldInfo"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomFieldShopID"] != null) && (set.Tables[0].Rows[0]["CustomFieldShopID"].ToString() != ""))
            {
                field.CustomFieldShopID = int.Parse(set.Tables[0].Rows[0]["CustomFieldShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomFieldCreateTime"] != null) && (set.Tables[0].Rows[0]["CustomFieldCreateTime"].ToString() != ""))
            {
                field.CustomFieldCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CustomFieldCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomFieldUserID"] != null) && (set.Tables[0].Rows[0]["CustomFieldUserID"].ToString() != ""))
            {
                field.CustomFieldUserID = int.Parse(set.Tables[0].Rows[0]["CustomFieldUserID"].ToString());
            }
            return field;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemCustomField ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.MemCustomField model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemCustomField set ");
            builder.Append("CustomType=@CustomType,");
            builder.Append("CustomField=@CustomField,");
            builder.Append("CustomFieldName=@CustomFieldName,");
            builder.Append("CustomFieldIsNull=@CustomFieldIsNull,");
            builder.Append("CustomFieldIsShow=@CustomFieldIsShow,");
            builder.Append("CustomFieldType=@CustomFieldType,");
            builder.Append("CustomFieldInfo=@CustomFieldInfo,");
            builder.Append("CustomFieldShopID=@CustomFieldShopID,");
            builder.Append("CustomFieldCreateTime=@CustomFieldCreateTime,");
            builder.Append("CustomFieldUserID=@CustomFieldUserID");
            builder.Append(" where CustomFieldID=@CustomFieldID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomType", SqlDbType.Int), new SqlParameter("@CustomField", SqlDbType.VarChar, 50), new SqlParameter("@CustomFieldName", SqlDbType.NVarChar, 50), new SqlParameter("@CustomFieldIsNull", SqlDbType.Bit, 1), new SqlParameter("@CustomFieldIsShow", SqlDbType.Bit, 1), new SqlParameter("@CustomFieldType", SqlDbType.VarChar, 50), new SqlParameter("@CustomFieldInfo", SqlDbType.VarChar, 500), new SqlParameter("@CustomFieldShopID", SqlDbType.Int, 4), new SqlParameter("@CustomFieldCreateTime", SqlDbType.DateTime), new SqlParameter("@CustomFieldUserID", SqlDbType.Int, 4), new SqlParameter("@CustomFieldID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CustomType;
            cmdParms[1].Value = model.CustomField;
            cmdParms[2].Value = model.CustomFieldName;
            cmdParms[3].Value = model.CustomFieldIsNull;
            cmdParms[4].Value = model.CustomFieldIsShow;
            cmdParms[5].Value = model.CustomFieldType;
            cmdParms[6].Value = model.CustomFieldInfo;
            cmdParms[7].Value = model.CustomFieldShopID;
            cmdParms[8].Value = model.CustomFieldCreateTime;
            cmdParms[9].Value = model.CustomFieldUserID;
            cmdParms[10].Value = model.CustomFieldID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }

        public int UpdateCustomFieldShow(string CustomFieldID, bool IsShow)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemCustomField set CustomFieldIsShow='" + IsShow + "'");
            if ((CustomFieldID != "") && (CustomFieldID != null))
            {
                builder.Append(" where CustomFieldID=" + int.Parse(CustomFieldID));
            }
            return DbHelperSQL.ExecuteSql(builder.ToString());
        }
    }
}

